package pers.rcl.utils;


/**
 * 生成唯一ID
 * 
 * @author lyq
 *
 */
public class IdUtils {
	private volatile static IdWorker instance;
	
	private static int workingId = 1;
	
	private IdUtils(){
		
	}
	
	public static IdWorker getInstance(long workerId) {  
        if (instance == null) {                           
            synchronized (IdWorker.class) {  
                if (instance == null) {         
                	instance = new IdWorker(workerId); 
                	//设置每个服务器的workingId，防止Id重复
                	workingId  = 1;
                }  
            }  
        }  
        return instance;  
    }
	
	/**
	 * @Description: 通过统一方式，生成有序的唯一ID
	 * @param: @return 
	 * @return: long
	 */
	public static synchronized long generateId() {
		return generateId(workingId);
	}

	/**
	 * 
	 * @Description: 通过统一方式，生成有序的唯一ID
	 * @param: @param worderId 服务器环境唯一标识
	 * @param: @return 
	 * @return: long
	 */
	public static synchronized long generateId(int worderId) {
		return IdUtils.getInstance(worderId).nextId();
	}

}

